package com.kedou.sdk.ui.view;

import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.kedou.sdk.params.BindPhoneResponseParams;
import com.kedou.sdk.utils.ResUtils;
import com.kedou.sdk.utils.WL;
import com.squareup.picasso.Picasso;

/**
 * Created by Administrator on 2017/6/6 0006.
 */

public class IDFaildDialog {

    final Builder builder;
    private final Dialog dialog;

    private IDFaildDialog(final Builder builder) {
        this.builder = builder;
        this.dialog = builder.dialog;
    }

    public void show() {
        if (dialog != null&& !dialog.isShowing()) {
            dialog.show();
        }
    }

    public void dismiss() {
        if (dialog != null&& dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public void setOnClickListener(View.OnClickListener listener){
        builder.setOnClickListener(listener);
    }

    public void setCancelable(boolean flag){
        if(dialog != null){
            dialog.setCancelable(flag);
        }
    }

    static class Builder implements View.OnClickListener {

        private Context context;
        private View contentView;
        private TextView nameTextView,copyTextView;
        private ImageView iconImage;
        private ImageView closeImage;
//        private Button confirm;

        private Dialog dialog;


        public Builder(final Context context) {
            this.context = context;
            final int layoutResId = ResUtils.getLayoutIdentifier(context, "wan_dialog_veryfy_faild_tip");
            final int nameResId = ResUtils.getViewIdentifier(context, "id_verify_name");
            final int iconResId = ResUtils.getViewIdentifier(context, "id_verify_image");
            final int closeResId = ResUtils.getViewIdentifier(context, "activity_payment_finish");
            final int copyResId = ResUtils.getViewIdentifier(context, "id_verify_copy");
            contentView = LayoutInflater.from(context).inflate(layoutResId, null);
            nameTextView = (TextView) contentView.findViewById(nameResId);
            iconImage = (ImageView) contentView.findViewById(iconResId);
            closeImage = (ImageView) contentView.findViewById(closeResId);
            closeImage.setOnClickListener(this);
            copyTextView = (TextView) contentView.findViewById(copyResId);
            copyTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //获取剪贴板管理器：
                    ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);

                    String temp = nameTextView.getText().toString().trim();
                    if(temp.contains("：")){
                        temp = temp.split("：")[1];
                    }
                    // 创建普通字符型ClipData
                    ClipData mClipData = ClipData.newPlainText("Label", temp);
                    // 将ClipData内容放到系统剪贴板里。
                    cm.setPrimaryClip(mClipData);
                    Toast.makeText(context,"复制成功",Toast.LENGTH_SHORT).show();
                }
            });
//            confirm = (Button) contentView.findViewById(confirmResId);
//            confirm.setOnClickListener(this);
            WL.e("ConfirmDialog","ConfirmDialog");
        }

        public Builder setMessage(BindPhoneResponseParams message) {
            if(message!=null){
                this.nameTextView.setText(message.getData().getContact_name());
                Picasso.with(context).load(message.getData().getContact_pic_url()).fit().into(iconImage);
            }
//            if (TextUtils.isEmpty(message)) {
//                this.nameTextView.setText("");
//            } else {
//                this.nameTextView.setText(message);
//            }
            return this;
        }

        public Builder setConfirmText(String text) {
//            if (!TextUtils.isEmpty(text)) {
//                this.confirm.setText(text);
//            }
            return this;
        }

        public Builder setOnClickListener(View.OnClickListener listener) {
//            this.confirm.setOnClickListener(listener);
            return this;
        }

        public IDFaildDialog create() {
            dialog = new Dialog(context, ResUtils.getStyleIdentifier(context, "hnssn_dialog"));
            dialog.setContentView(contentView);
            dialog.setCanceledOnTouchOutside(false);
            return new IDFaildDialog(this);
        }

        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    }


}
